<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mindex extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getAdv()
    {
        ini_set('mongo.native_long', 1);
        require_once(PROJECT_PATH.'/getinfo/index.php');
        $info = new UAParser;
        
        $ua = $_SERVER['HTTP_USER_AGENT'];
        $result = $info->parse($ua);
        
        $BrowerName         = $result->ua->family;
        @$exp_Os             = explode(' ',$result->os->family);
        @$OsName             = $exp_Os[0];
        $NameDevice         = $result->device->family;
        
        $BrowerNameVer     = $result->ua->toString;
        $OsNameVer         = $result->os->toString;
        $NameDeviceVer     = $result->device->family;
        
        $Ip             = '192.168.1.2';//$this->get_client_ip();
        
        $now = date("Y-m-d H:i:s", time());
        
        $id_shop = $this->db->query('SELECT id FROM shop WHERE ip = "'.$Ip.'"')->row_object()->id;
       
        $data = $this->db->query('SELECT id,folder_campaign FROM campaign WHERE id_shop = '.$id_shop.' ORDER BY id DESC LIMIT 1')->row_object();//->folder_campaign;
        
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $mac = $this->getParamString('mac');
        $called = $this->getParamString('called');
        
        $arr_log = array(
                    'browser_name'          => $BrowerNameVer, 
                    'browser_name_group'    => $BrowerName,
                    'os_name'               => $OsNameVer, 
                    'os_group'              => $OsName,
                    'device_name'           => $NameDeviceVer, 
                    'ip_access'             => $Ip,
                    'id_shop'               => $id_shop,
                    'hours_access'          => intval(date('H')),
                    'minutes_access'        => intval(date('i')),
                    'seconds_access'        => intval(date('s')),
                    'day_access'            => intval(date('d')),
                    'month_access'          => intval(date('m')),
                    'year_access'           => intval(date('Y')),
                    'id_campaign'           => intval($data->id),
                    'time_access'           => date('Y-m-d H:i:s'),
                    'show_time'             => date('d-m-Y'),
                    'show_full_time'        => date('H:i:s d-m-Y'),
                    'time_vs'               => $cd__,
                    'mac'                   => $mac,
                    'called'                => $called
                );
        $this->m_db->insert('log_access', $arr_log);
        
        $check_mac = $this->db->query('SELECT id FROM mac WHERE id_campaign = '.$data->id.' AND id_shop = '.$id_shop.' AND mac_physical = "'.$mac.'"')->num_rows();
        if($check_mac == 0)
        {
            $arc = array(
                    'id_campaign'   => $data->id,
                    'id_shop'       => $id_shop,
                    'mac_physical'  => $mac,
                    'called'        => $called
                );
            $this->db->insert('mac', $arc);
        }
        return $data;
        
        
    }
    
    public function activeAdv()
    {
        $now = date("Y-m-d H:i:s", time());
        $sql = "SELECT id FROM campaign
                    WHERE active = 0 AND (time_start <= '".$now."' AND time_end >= '".$now."')";
        $data = $this->db->query($sql)->result_object();
        if(!in_array($data, array(null, '', '0')))
        {
            foreach($data AS $el=>$le)
            {
                $this->db->update('campaign', array('active' => 1), "id = ".$le->id);
                $count = $this->db->affected_rows();
                if($count)
                {
                    echo 'OK';
                }
            }
        }
    }
    
    public function complateAdv()
    {
        $now = date("Y-m-d H:i:s", time());
        $sql = "SELECT id FROM campaign
                    WHERE time_start <= '".$now."' AND time_end <= '".$now."'";
        $data = $this->db->query($sql)->result_object();
        if(!in_array($data, array(null, '', '0')))
        {
            foreach($data AS $el=>$le)
            {
                $this->db->update('campaign', array('active' => 3), "id = ".$le->id);
                $count = $this->db->affected_rows();
                if($count)
                {
                    echo 'OK';
                }
            }
        }
    }
    
}